import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import mean_squared_error
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score
import matplotlib.pyplot as plt
import seaborn as sns
from imblearn.over_sampling import RandomOverSampler
from collections import Counter
import numpy as np
# 读取CSV文件（修正文件路径）
data = pd.read_csv('./data/情感文本分析/sentimentdataset.csv')
data['Text'] = data['Text'].str.split('.').str[:3].apply(lambda x: '. '.join([sent + '.' for sent in x]))
sentiment_labels = data['Sentiment']
text_data = data['Text']

# 2. 词汇表示：TF-IDF向量化器
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(text_data)

# 3. 数据平衡：使用重采样技术平衡数据
counter = Counter(sentiment_labels)
print(f"Original dataset shape {Counter(sentiment_labels)}")
ros = RandomOverSampler(random_state=42)
X_resampled, y_resampled = ros.fit_resample(X, sentiment_labels)
print(f'Resampled dataset shape {Counter(y_resampled)}')

# 4. 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.3, random_state=42)

# 5. 文本分类：线性SVM分类器
clf = LinearSVC(random_state=42, dual=False)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)


# 假设分类任务：根据销售额的平均值进行分类
arima_predictions_class = (y_pred > text_data).astype(int)
table_data = [
    ["Model", "Accuracy", "Recall", "Precision", "F1 Score"],
    ['ARIMA', f"{accuracy_score(arima_predictions_class, arima_predictions_class):.2f}",
     f"{recall_score(arima_predictions_class, arima_predictions_class):.2f}",
     f"{precision_score(arima_predictions_class, arima_predictions_class):.2f}",
     f"{f1_score(arima_predictions_class, arima_predictions_class):.2f}"]
]

fig, ax = plt.subplots(figsize=(10, 7))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=table_data, loc='center', cellLoc='center')
the_table.auto_set_font_size(False)
the_table.set_fontsize(14)
the_table.auto_set_column_width(
    col=list(range(len(["Model", "Accuracy", "Recall", "Precision", "F1 Score", "MSE", "RMSE"]))))
# 设置表格的列宽和行高为自适应
for (i, j), cell in the_table.get_celld().items():
    cell.set_text_props(fontproperties=plt.matplotlib.font_manager.FontProperties(weight='bold'))
plt.legend()
# plt.show()
plt.savefig('Text.png')  # 保存图像

# 6. 评估与可视化
accuracy = accuracy_score(y_test, y_pred)
print(f"SVM模型准确率: {accuracy:.4f}")
print(classification_report(y_test, y_pred))

cm = confusion_matrix(y_test, y_pred)
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]  # 归一化
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, cmap='Blues', fmt='.2f')
plt.xlabel('Predicted Sentiment')
plt.ylabel('Actual Sentiment')
plt.title('Sentiment Classification Confusion Matrix')
plt.show()